<template>
  <div class="formplane">
    <el-form ref="elForm" :model="formData" size="medium" label-width="100px">
      <el-form-item label="机构名称">
        <el-input v-model="formData.forganname" placeholder="请输入机构名称" clearable :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item label="上级机构">
        {{ formData.fparentname }}
      </el-form-item>
      <el-form-item label="机构编码">
        {{ formData.forgancode }}
      </el-form-item>
      <el-form-item label="顺序号">
        <el-input-number v-model="formData.forderindex" placeholder="请输入顺序号" :step="1" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="传真">
        <el-input v-model="formData.ffax" placeholder="请输入传真" clearable :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="formData.fphone" placeholder="请输入电话" clearable :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input type="textarea" v-model="formData.faddress" placeholder="请输入地址" :rows="2" :style="{ width: '100%' }"></el-input>
      </el-form-item>
      <el-form-item size="large" class="buttonitem">
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getOrgan, SaveOrgan } from '@/api/tappOrgan'
export default {
  components: {},
  props: ['datainfo'],
  data () {
    return {
      formData: {
        forganname: undefined,
        fparentname: undefined,
        ffax: undefined,
        forderindex: 0,
        fphone: undefined,
        faddress: undefined,
        fparentid: undefined,
        fid: undefined
      }
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () {
    if (this.datainfo !== undefined && this.datainfo !== null) {
      if (this.datainfo.fid !== undefined && this.datainfo.fid !== null) {
        getOrgan(this.datainfo.fid).then(res => {
          if (res.code === 0) {
            this.formData.forganname = res.data.forganname
            this.formData.fparentname = this.datainfo.fparentname
            this.formData.ffax = res.data.ffax
            this.formData.forderindex = res.data.forderindex
            this.formData.fphone = res.data.fphone
            this.formData.faddress = res.data.faddress
            this.formData.fparentid = res.data.fparentid
            this.formData.fid = res.data.fid
            this.formData.forgancode = res.data.forgancode
          }
        })
      } else {
        Object.assign(this.formData, this.datainfo)
      }
    }
  },
  methods: {
    submitForm () {
      if (
        this.formData.forganname === '' ||
        this.formData.forganname === undefined
      ) {
        this.$message.success('请输入机构名称')
        return
      }
      if (
        this.formData.forderindex === '' ||
        this.formData.forderindex === undefined
      ) {
        this.$message.success('请输入顺序号')
        return
      }
      SaveOrgan(this.formData).then(res => {
        if (res.code === 0) {
          this.formData.fid = res.data.fid
          this.formData.fcode = res.data.forgancode
          this.$emit('savedata', this.formData)
          this.$message.success('保存成功！')
        }
      })
    },
    resetForm () {
      this.formData.forganname = undefined
      this.formData.ffax = undefined
      this.formData.forderindex = 0
      this.formData.fphone = 0
      this.formData.faddress = undefined
    }
  }
}
</script>
<style scoped>
.formplane {
  margin: 15px;
}
.buttonitem >>> .el-form-item__content {
  float: right;
  height: 100%;
}
</style>
